package com.zpark.service.impl;

import com.zpark.entity.Books;
import com.zpark.exception.InsertBooksException;
import com.zpark.exception.SelectByIdBooksException;
import com.zpark.mapper.IBooksMapper;
import com.zpark.service.IBooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PackageName: com.zpark.service.impl
 * @ClassName: BooksServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2023/5/8  15:58
 */
@Service
public class BooksServiceImpl implements IBooksService {

    @Autowired
    private IBooksMapper booksMapper;

    @Override
    public Integer addBooks(Books books) {
        Integer row = booksMapper.insertBooks(books);
        if (row <= 0) {
            // 添加数据失败
            throw new InsertBooksException("添加数据失败");
        }
        return row;
    }

    @Override
    public Integer delBooksById(Integer id) {
        return booksMapper.deleteBooksById(id);
    }

    @Override
    public Integer changeBooksById(Books books) {
        return booksMapper.updateBooksById(books);
    }

    @Override
    public List<Books> findAll() {
        return booksMapper.selectAll();
    }

    @Override
    public List<Books> findBooksByLikeName(String bName) {
        return booksMapper.selectBooksByLikeName("%" + bName + "%");
    }

    @Override
    public Books findById(Integer id) {
        Books book = booksMapper.selectById(id);
        if (book == null) {
            // 查询数据失败（没有查找到数据）
            throw new SelectByIdBooksException("查询数据失败（没有查找到数据）");
        }
        return book;
    }
}
